package parsers;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import object.Department;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class DepartmentParser {
	
	private Department department;
	
	public DepartmentParser(){
		
	}
	
	public DepartmentParser(Department department){
		this.department=department;
	}
	
	public void setDepartment(Department department){
		this.department=department;
	}
	
	public Element parse(Document doc){
		if(doc==null||department==null){
			System.err.println("doc or department cannot be null!");
			return null;
		}
		String did=department.getDid();
		String dname=department.getDname();
		Calendar establishment=department.getEstablishment();
		String dinfo=department.getInfo();
		
		Element departmentE=doc.createElement("部门信息");
		
		Element didE=doc.createElement("部门编号");
		Text didT=doc.createTextNode(did);
		departmentE.appendChild(didE);
		didE.appendChild(didT);
		
		Element dnameE=doc.createElement("部门名称");
		Text dnameT=doc.createTextNode(dname);
		departmentE.appendChild(dnameE);
		dnameE.appendChild(dnameT);
		
		if(establishment!=null){
			Element establishmentE=doc.createElement("成立日期");
			Text establishmentT=doc.createTextNode(new SimpleDateFormat("yyyy-MM-dd").format(establishment.getTime()));
			departmentE.appendChild(establishmentE);
			establishmentE.appendChild(establishmentT);
		}
		
		if(dinfo!=null){
			Element dinfoE=doc.createElement("部门描述");
			Text dinfoT=doc.createTextNode(dinfo);
			departmentE.appendChild(dinfoE);
			dinfoE.appendChild(dinfoT);
		}
		
		return departmentE;
	}
	
	

}
